/**
 * 城市、区县、商圈选择框
 * author: royoan
 * version: 1.0
 * since: 2015-2-3
 */
;(function ($,window,document,undefined) {
	
$.fn.address = function(options) {
    var defaults = {
    	'provinceid': 'provinceid',//商圈的下拉框表单id属性
        'cityid': 'cityid',//商圈的下拉框表单id属性
        'areaid': 'areaid',//商圈的下拉框表单id属性 
        'circleid': 'circleid' //商圈的下拉框表单id属性(可选)
    };
    //把options的值设置给default，然后赋值给settings
    var settings = $.extend({}, defaults, options);
    
    //初始化区县下拉框
    	var select_province = $(this).val();
//    	var ae = $("#" + settings.cityid);
//    	ae.empty();
//    	ae.append("<option value=''>正在查询区县...</option>");
//        $.getJSON(ctx + "/life/region/getCityByProvinceid.action", {cityid:select_cityid}, function (area_list) {
//        	ae.empty();
//        	for (var i=0;i<area_list.length; i++) {
//        		ae.append("<option value='" + area_list[i].id + "'>" + area_list[i].name + "</option>");
//    	   } 
//        });
        
        
    	var citys = $("#" + settings.cityid);
    	citys.empty();
    	var areas = $("#" + settings.areaid);
    	areas.empty();
    	var circle = $("#" + settings.circleid);
    	circle.empty();
        $.getJSON(ctx + "/life/region/getCityByProvinceid.action", {provinceid : select_province}, function (returnObj) {
           for (var i=0;i<returnObj.citys.length; i++) {
        	   	var city = returnObj.citys[i];
        	   	citys.append("<option value='" + city.id + "'>" + city.name + "</option>");
    	   }
           areas.append("<option value=''>--请选择--</option>");
           for (var i=0;i<returnObj.areas.length; i++) {
        	   	var area = returnObj.areas[i];
        	   	areas.append("<option value='" + area.id + "'>" + area.name + "</option>");
    	   }
           circle.append("<option value=''>--请选择--</option>");
        });
    
    
    $(this).bind("change", function(){
    	var select_cityid = $(this).val();
    	var citys = $("#" + settings.cityid);
    	citys.empty();
    	citys.append("<option value=''>正在查询市区...</option>");
    	
    	var areas = $("#" + settings.areaid);
    	areas.empty();
    	var circle = $("#" + settings.circleid);
    	circle.empty();
    	
        $.getJSON(ctx + "/life/region/getCityByProvinceid.action", {cityid:select_cityid}, function (area_list) {
        	ae.empty();
        	for (var i=0;i<area_list.citys.length; i++) {
        		var city = area_list.citys[i];
        		citys.append("<option value='" + city.id + "'>" + city.name + "</option>");
        	}
	        areas.append("<option value=''>--请选择--</option>");
	        for (var i=0;i<area_list.areas.length; i++) {
	        	 var area = area_list.areas[i];
	        	 areas.append("<option value='" + area.id + "'>" + area.name + "</option>");
	    	}
        	circle.append("<option value=''>--请选择--</option>");
        });
    });
    
    $('#' + settings.cityid).bind("change", function(){
    	var select_cityid = $(this).val();
    	var ae = $("#" + settings.areaid);
    	ae.empty();
    	ae.append("<option value=''>正在查询区县...</option>");
    	var circle = $("#" + settings.circleid);
    	circle.empty();
        $.getJSON(ctx + "/life/region/getAreaByCityid.action", {cityid:select_cityid}, function (area_list) {
        	ae.empty();
        	ae.append("<option value=''>--请选择区县--</option>");
        	for (var i=0;i<area_list.length; i++) {
        		ae.append("<option value='" + area_list[i].id + "'>" + area_list[i].name + "</option>");
        	} 
        	circle.append("<option value=''>--请选择--</option>");
        });
    });
    
    $('#' + settings.areaid).bind("change", function(){
    	var select_areaid= $(this).val();
    	var ae = $("#" + settings.circleid);
    	ae.empty();
    	ae.append("<option value=''>正在查询商圈...</option>");
        $.getJSON(ctx + "/life/busicircle/getCircleByAreaid.action", {areaid:select_areaid}, function (circle_list) {
        	ae.empty();
        	ae.append("<option value=''>--请选择商圈--</option>");
			   for (var i=0;i<circle_list.length; i++) {
				ae.append("<option value='" + circle_list[i].id + "'>" + circle_list[i].name + "</option>");
			   } 
        });
    });
}

})(jQuery, window, document);